package edu.osumc.bmi.service.consumer;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.ws.client.core.WebServiceTemplate;

import edu.osumc.bmi.soap.schema.AddUserRequest;
import edu.osumc.bmi.soap.schema.DeleteUserRequest;
import edu.osumc.bmi.soap.schema.GetAllUsersRequest;
import edu.osumc.bmi.soap.schema.GetAllUsersResponse;
import edu.osumc.bmi.soap.schema.GetUserRequest;
import edu.osumc.bmi.soap.schema.GetUserResponse;
import edu.osumc.bmi.soap.schema.UpdateUserRequest;
import edu.osumc.bmi.soap.schema.User;

@Component("soapClient")
public class SoapClientImpl implements SoapClient {
	@Autowired
	@Qualifier("webServiceTemplate")
	private WebServiceTemplate serviceTemplate;

	public void addUser(User user) {
		AddUserRequest request = new AddUserRequest();
		request.setUser(user);
		serviceTemplate.marshalSendAndReceive(request);
	}

	public void deleteUser(Integer id) {
		DeleteUserRequest request = new DeleteUserRequest();
		request.setId(id);
		serviceTemplate.marshalSendAndReceive(request);
	}

	public List<User> getAllUsers() {
		GetAllUsersRequest request = new GetAllUsersRequest();
		GetAllUsersResponse response = (GetAllUsersResponse) serviceTemplate
				.marshalSendAndReceive(request);
		return response.getUser();

	}

	public User getUser(Integer id) {
		GetUserRequest request = new GetUserRequest();
		request.setId(id);
		GetUserResponse response = (GetUserResponse) serviceTemplate
				.marshalSendAndReceive(request);
		return response.getUser();
	}

	public void updateUser(User user) {
		UpdateUserRequest request = new UpdateUserRequest();
		request.setUser(user);
		serviceTemplate.marshalSendAndReceive(request);
	}

}
